﻿using PLCConsole;
using PLCConsole.PLCConsole;
using PLCConsole.PLCSerStation;
using StationNF;
using StationSer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DExSerConsoleV1
{
    public partial class FOpenPLCConsole : Form
    {
        IFDExCommunicater tDestCommunicater { get; set; } = null;
        IFStationsServer tStationsServer { get; set; } = null;
        string StationID { get; set; } = "";
        public FOpenPLCConsole(IFStationsServer tDestStaServer,string sStaID)
        {
            InitializeComponent();
            tStationsServer = tDestStaServer;
            StationID = sStaID;


            comboBoxPLCType.Items.Clear();
            foreach(StationPLCType iPLCType in Enum.GetValues(typeof(StationPLCType)))
            {
                comboBoxPLCType.Items.Add(string.Format("{0}:{1}",
                    ((int)iPLCType).ToString(),
                    iPLCType.ToString()));
            }
            if ((tStationsServer != null) && (!string.IsNullOrEmpty(StationID)))
            {
                tDestCommunicater = tDestStaServer.getCommunicater(StationID);
                if (tDestCommunicater != null)
                {
                    labelLastAction.Text = tDestCommunicater.LastPacketTime.ToString("HH:mm:ss");
                    textBoxStaID.Text = tDestCommunicater.StationID;

                    IFStation tPLCStation = tDestCommunicater.Station;
                    if (tPLCStation != null)
                    {
                        comboBoxPLCType.SelectedIndex = 0;
                        if ((tPLCStation.StationType > 0) && (tPLCStation.StationType <= comboBoxPLCType.Items.Count + 1))
                        {
                            comboBoxPLCType.SelectedIndex = tPLCStation.StationType;
                        }
                        labelPLCRegInfo.Text = string.Format("{0}:{1}",
                                    tPLCStation.StationRegStar,
                                tPLCStation.StationRegCount);
                    }
                    else
                        labelPLCRegInfo.Text = "";
                    if (!string.IsNullOrEmpty(labelPLCRegInfo.Text))
                    {
                        buttonOpenPLC.Enabled = true;
                    }
                }
            }
        }

        private void buttonOpenPLC_Click(object sender, EventArgs e)
        {
            if (comboBoxPLCType.SelectedIndex >= 0)
            {
                switch ((StationPLCType)comboBoxPLCType.SelectedIndex)
                {
                    case StationPLCType.WFDRVX:
                        FPLCInfo_WFDRVX tFPLCInfoWFDRVX = new FPLCInfo_WFDRVX(new Station_WFDRVX(tStationsServer,StationID));
                        Close();
                        tFPLCInfoWFDRVX.ShowDialog();
                        break;
                    case StationPLCType.ADP2203VX:
                        FPLCInfo_ADP2203VX tFPLCInfoADP2203VX = new FPLCInfo_ADP2203VX(new Station_ADP2203VX(tStationsServer, StationID));
                        Close();
                        tFPLCInfoADP2203VX.Show();
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
